<?php
	require_once("Database.php");

	class Announcement{
		private $id=0;
		private $owner =0;
		private $title="";
		private $description="";
		private $location=0;
		private $category=0;
		private $salary=0.0;
		private $updateDate="";
		private $saveDate="";
		
		private $result="";
		private $ownerName="";
		private $locationName="";
		private $categoryName="";
		
		public function setId($id){
			$this->id = $id;
		}
		
		public function setOwner($owner){
			$this->owner = $owner;
		}
		
		public function setTitle($title){
			$this->title = $title;
		}
		
		public function setDescription($description){
			$this->description = $description;
		}
		
		public function setLocation($location){
			$this->location = $location;
		}
		
		public function setCategory($category){
			$this->category = $category;
		}
		
		public function setSalary($salary){
			$this->salary = $salary;
		}
		
		public function setUpdateDate($update){
			$this->updateDate = $update;
		}
		
		public function setSaveDate($save){
			$this->saveDate = $save;
		}
		
		public function setOwnerName($name){
			$this->ownerName = $name;
		}
		
		public function setLocationName($name){
			$this->locationName = $name;
		}
		
		public function setCategoryName($name){
			$this->categoryName = $name;
		}
		
		public function getId(){
			return $this->id;
		}
		
		public function getOwner(){
			return $this->owner;
		}
		
		public function getTitle(){
			return $this->title;
		}
		
		public function getDescription(){
			return $this->description;
		}
		
		public function getLocation(){
			return $this->location;
		}
		
		public function getCategory(){
			return $this->category;
		}
		
		public function getSalary(){
			return $this->salary;
		}
		
		public function getUpdateDate(){
			return $this->updateDate;
		}
		
		public function getSaveDate(){
			return $this->saveDate;
		}
		
		public function getOwnerName(){
			return $this->ownerName;
		}
		
		public function getLocationName(){
			return $this->locationName;
		}
		
		public function getCategoryName(){
			return $this->categoryName;
		}
		
		public function setValues($id, $owner, $title, $description, $location, $category, $salary){
			$this->id = $id;
			$this->owner = $owner;
			$this->title = $title;
			$this->description = $description;
			$this->location = $location;
			$this->category = $category;
			$this->salary = $salary;
		}
		
		public function findById($id){
			$db = new Database("localhost", "root", "");
			$db->connect("emprego");
			$db->executeQuery("SELECT * FROM announcement WHERE id='$id'");
			
			if($line = $db->getResultLine(MYSQL_ASSOC)){
				$this->setValues($line["id"], $line["owner"], $line["title"], $line["description"],
									$line["location"], $line["category"], $line["salary"]);
				$this->setUpdateDate($line["update_date"]);
				$this->setSaveDate($line["savedate"]);
				$db->disconnect();
				return TRUE;
			}else{
				$db->disconnect();
				return FALSE;
			}
		}
		
		public function findByTitle($title){
			if($this->result == ""){
				$db = new Database("localhost", "root", "");
				$db->connect("emprego");
				$db->executeQuery("SELECT a.id, a.owner, a.title, a.description, a.location, a.category, a.salary, a.update_date, a.savedate,
									u.nome as enterprise, l.name as loc, c.name as cat
									FROM announcement a, users u, location l, category c
									WHERE a.owner=u.id AND a.location=l.id AND a.category=c.id AND a.title LIKE '$title'");
				$this->result = $db->getResult();
				$db->disconnect();
			}
			if($line = mysql_fetch_array($this->result, MYSQL_ASSOC)){
				$this->setValues($line["id"], $line["owner"], $line["title"], $line["description"],
									$line["location"], $line["category"], $line["salary"]);
				$this->setUpdateDate($line["update_date"]);
				$this->setSaveDate($line["savedate"]);
				$this->setOwnerName($line["enterprise"]);
				$this->setLocationName($line["loc"]);
				$this->setCategoryName($line["cat"]);
				return TRUE;					
			}else{
				$this->result = "";
				return FALSE;
			}
		}
		
		public function findByOwner($ownerId){
			if($this->result == ""){
				$db = new Database("localhost", "root", "");
				$db->connect("emprego");
				$db->executeQuery("SELECT a.id, a.owner, a.title, a.description, a.location, a.category, a.salary, a.update_date, a.savedate,
									u.nome as enterprise, l.name as loc, c.name as cat
									FROM announcement a, users u, location l, category c
									WHERE a.owner=u.id AND a.location=l.id AND a.category=c.id AND a.owner='$ownerId'");
				$this->result = $db->getResult();
				$db->disconnect();
			}
			if($line = mysql_fetch_array($this->result, MYSQL_ASSOC)){
				$this->setValues($line["id"], $line["owner"], $line["title"], $line["description"],
									$line["location"], $line["category"], $line["salary"]);
				$this->setUpdateDate($line["update_date"]);
				$this->setSaveDate($line["savedate"]);
				$this->setOwnerName($line["enterprise"]);
				$this->setLocationName($line["loc"]);
				$this->setCategoryName($line["cat"]);
				return TRUE;					
			}else{
				$this->result = "";
				return FALSE;
			}
		}
		
		public function save(){
			$db = new Database("localhost", "root", "");
			$db->connect("emprego");
			
			$db->executeQuery("SELECT id FROM announcement WHERE id='$this->id'");
			if($line = $db->getResultLine(MYSQL_ASSOC)){
				$db->executeQuery("UPDATE announcement SET owner='$this->owner', title='$this->title', description='$this->description',
									location='$this->location', category='$this->category', salary='$this->salary', update_date= now()");
				return 1;
			}else{
				$db->executeQuery("INSERT INTO announcement(owner, title, description, location, category, salary, update_date, savedate)
									VALUES('$this->owner', '$this->title', '$this->description', '$this->location', 
									'$this->category', '$this->salary', now(), now())");
				return 2;
			}
		}
	}
?>
